﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atomic.Db;
using Atomic.User.Entities;
using NHibernate;

namespace Atomic.User.DAL
{
    /// <summary>
    /// 用户类型扩展字段管理，包括管理扩展表中的字段信息，支持SQLServer2000+
    /// </summary>
    public class UserExtendFieldDAL : BaseDAL<UserExtendFieldEntity>
    {
        public UserExtendFieldDAL() : base() { }
        /// <summary>
        /// 用于事务的时候
        /// </summary>
        /// <param name="session"></param>
        public UserExtendFieldDAL(ISession session) : base(session) { }

        /// <summary>
        /// 更新扩展字段信息：显示名称、字段长度、类型、备注 可以被更新
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="id"></param>
        public override void Update(UserExtendFieldEntity entity, object id)
        {
            UserExtendFieldEntity empEntity = session.Get<UserExtendFieldEntity>(id);
            //更新字段赋值
            empEntity.DisplayName = entity.DisplayName;
            empEntity.Length = entity.Length;
            empEntity.Comment = entity.Comment;
            empEntity.FieldType = entity.FieldType;
            base.Update(entity, id);
        }

        /// <summary>
        /// 增加新的用户扩展信息列
        /// </summary>
        /// <param name="entity">扩展列信息</param>
        public virtual void AddUserExtendField(UserExtendFieldEntity entity)
        {
            string sql = "Alter TABLE [dbo].[UserExtend{0}] Add {1} {2}";   //增加新的列语句
            switch (entity.FieldType.ToLower())
            {
                //整型数据库字段类型
                case "int":
                    {
                        sql = string.Format(sql, entity.UserTypeID, entity.Name, "int");
                        break;
                    }
                //字符串类型，对应数据库的nvarchar类型
                case "string":
                    {
                        sql = string.Format(sql, entity.UserTypeID, entity.Name, string.Format("nvarchar({0})", entity.Length));
                        break;
                    }
                default:
                    {
                        throw new Exception("不支持的字段类型：" + entity.FieldType);
                    }
            }
            session.CreateSQLQuery(sql).ExecuteUpdate();  //创建新的用户类型对于的扩展属性表
        }

        /// <summary>
        /// 删除用户扩展信息列
        /// </summary>
        /// <param name="userTypeID">用户类型</param>
        /// <param name="name">字段名</param>
        public virtual void DropUserExtendField(int userTypeID, string name)
        {
            string sql = "Alter TABLE [dbo].[UserExtend{0}] Drop Column {1}";   //删除列语句
            session.CreateSQLQuery(string.Format(sql, userTypeID, name)).ExecuteUpdate();  //删除列
        }

        /// <summary>
        /// 更新指定用户类型扩展表字段部分信息：字段类型、长度
        /// </summary>
        /// <param name="entity">字段更新信息</param>
        public virtual void UpdateUserExtendField(UserExtendFieldEntity entity)
        {
            string sql = "Alter TABLE [dbo].[UserExtend{0}] Alter Column {1} {2}";   //修改字段语句
            switch (entity.FieldType.ToLower())
            {
                //整型数据库字段类型
                case "int":
                    {
                        sql = string.Format(sql, entity.UserTypeID, entity.Name, "int");
                        break;
                    }
                //字符串类型，对应数据库的nvarchar类型
                case "string":
                    {
                        sql = string.Format(sql, entity.UserTypeID, entity.Name, string.Format("nvarchar({0})", entity.Length));
                        break;
                    }
                default:
                    {
                        throw new Exception("不支持的字段类型：" + entity.FieldType);
                    }
            }
            session.CreateSQLQuery(sql).ExecuteUpdate();  //创建新的用户类型对于的扩展属性表
        }

        /// <summary>
        /// 指定用户类型的指定字段是否已经至少有一条记录该字段有值
        /// </summary>
        /// <param name="userTypeID">用户类型</param>
        /// <param name="fieldName">字段名称</param>
        /// <returns>是否为空</returns>
        public virtual bool IsFieldValueEmpty(int userTypeID, string fieldName)
        {
            string sql = "Select Top 1 * From UserExtend{0} Where ISNULL(" + fieldName + ",'')<>''";   //查询删除字段是否为空语句
            return session.CreateSQLQuery(sql).List<object>().Count == 0;  //指定字段是否已经在使用中
        }
    }
}